// pages/active/active.js
const app = getApp()
const promisify = require("../../utils/promisify")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tags: [{
        name: "全部",
        id: 1
      },
      {
        name: "志愿",
        id: 2
      },
      {
        name: "学术",
        id: 3
      },
      {
        name: "娱乐",
        id: 4
      }
    ],
    activities: [[]],
    selectedTag: 1,
    selectedActivity: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    promisify(wx.request)({
      url: `https://www.cumthing.world/cumthing/activity-info/list`,
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 默认值
      }
    }).then(
      data => {
        let findTag = "activities[0]"
        that.setData({
          [findTag]: data.data.data.list
        })
        that.changeView()
      }
    )
  },
  changeView: function () {
    /**
     * 通过不同标签，显示不同活动
     */
    let that = this
    let activities = that.data.activities
    let selectedTag = that.data.selectedTag
    if (!activities[selectedTag - 1]) {
      promisify(wx.request)({
        url: `https://www.cumthing.world/cumthing/activity-info/list2?categoryId=` + selectedTag,
        method: "GET"
      }).then(
        data => {
          let findTag = "activities[" + (selectedTag - 1) + "]"
          that.setData({
            [findTag]: data.data.data.list
          })
          that.setData({
            selectedActivity: that.data.activities[selectedTag - 1]
          })
        }
      )
    } else {
      that.setData({
        selectedActivity: that.data.activities[selectedTag - 1]
      })
    }
  },
  tagChange: function (e) {
    /**
     * 标签的点击事件，会调用changeView来改变界面
     */
    let that = this
    let tag = e.currentTarget.dataset.text
    that.setData({
      selectedTag: tag
    })
    that.changeView()
  }
})